package com.qf.controller.show;

import com.qf.controller.BaseController;
import com.qf.pojo.Page;
import com.qf.pojo.StuInfo;
import com.qf.service.ShowService;
import com.qf.service.impl.ShowServiceImpl;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 沐辰
 * @version v1.0
 * @description:
 * @project _4.30_Exam_two
 * @Date 2024/4/30 10:29
 * @company qf
 */
@WebServlet(name = "ShowController",urlPatterns = "/show")
public class ShowController extends BaseController {

    private ShowService showService = new ShowServiceImpl();

    /**
     * 管理系统的添加学生功能
     * @param request
     * @param response
     * @return
     */
    public String insertMsg(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String stuname = request.getParameter("stuname");
        String stuno = request.getParameter("stuno");
        String pwd = request.getParameter("pwd");
        String idcard = request.getParameter("idcard");
        String date = request.getParameter("startdate");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startdate = simpleDateFormat.parse(date);
        String status = request.getParameter("status");
        //封装属性
        StuInfo stuInfo = new StuInfo();
        stuInfo.setStuName(stuname);
        stuInfo.setStuNo(stuno);
        stuInfo.setPwd(pwd);
        stuInfo.setIdCard(idcard);
        stuInfo.setStartDate(startdate);
        stuInfo.setStatus(Integer.valueOf(status));
        boolean flag = showService.insertMsg(stuInfo);
        if (flag) {
            return "forward:/successAdmin.jsp";
        }
        return "forward:/index.jsp";
    }

    /**
     * 查看管理端学生信息
     * @param request
     * @param response
     * @return
     */
    public String showMsg(HttpServletRequest request, HttpServletResponse response) {
        int page = Integer.valueOf(request.getParameter("page"));
        Page<StuInfo> stuInfoPage = showService.showMsg(page);
        if (stuInfoPage != null) {
            stuInfoPage.getPageMsg().forEach(stuInfo -> {
                if (stuInfo.getStatus()==1) {
                    request.setAttribute("qx","管理员");
                }else {
                    request.setAttribute("qx","普通用户");
                }
            });
            request.setAttribute("stuInfo",stuInfoPage);
            return "forward:/listAdmin.jsp";
        }
        return null;
    }

    /**
     * 管理端删除功能实现
     * @param request
     * @param response
     * @return
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = showService.deleteById(id);
        if (flag) {
            return "forward:/show?method=showMsg&page=1";
        }
        return null;
    }

    /**
     * 修改个人信息页面回显
     * @param request
     * @param response
     * @return
     */
    public String update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        StuInfo stuInfo = showService.selectById(id);
        stuInfo.setPwd("******");
        if (stuInfo != null) {
            request.setAttribute("stu",stuInfo);
            return "forward:/update.jsp";
        }
        return null;
    }

    /**
     * 修改信息
     * @param request
     * @param response
     * @return
     */
    public String updateMsg(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        int id = Integer.parseInt(request.getParameter("id"));
        String stuName = request.getParameter("stuName");
        String stuNo = request.getParameter("stuNo");
        String pwd = request.getParameter("pwd");
        String idCard = request.getParameter("idCard");
        String date = request.getParameter("startDate");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startdate = simpleDateFormat.parse(date);
        StuInfo stuInfo = new StuInfo();
        stuInfo.setId(id);
        stuInfo.setStuName(stuName);
        stuInfo.setStuNo(stuNo);
        stuInfo.setPwd(pwd);
        stuInfo.setIdCard(idCard);
        stuInfo.setStartDate(startdate);
        boolean flag = showService.updateById(stuInfo);
        if (flag) {
            return "forward:/success.jsp";
        }
        return null;
    }
}
